package top.lixb.libcommon.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.vondear.rxtool.RxLogTool;
import com.vondear.rxtool.view.RxToast;

import java.util.ArrayList;
import java.util.List;

import top.lixb.libcommon.R;
import top.lixb.libcommon.log.L;

public abstract class RefreshListFragment<T> extends BaseFragment {

    protected RecyclerView mRv;
    protected SmartRefreshLayout mSrl;
    protected CommonAdapter mCommonAdapter;
    protected int mPage = 1;
    protected int mPageSize = 10;
    protected boolean isrefresh;


    @Override
    protected void initView(View view) {
        mSrl = view.findViewById(R.id.srl);
        mRv = view.findViewById(R.id.rv);
        mRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        mCommonAdapter = new CommonAdapter(getItemLayout(),new ArrayList<T>());
        mRv.setAdapter(mCommonAdapter);
        mSrl.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                L.e("CZF::"+"onloadmore");
                isrefresh = false;
                getData(isrefresh);
                if (null != mSrl) {
                    mSrl.finishLoadMore(2000);
                }
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                L.e("CZF::"+"onRefresh");
                isrefresh = true;
                mPage = 1;
                getData(isrefresh);
                if (null != mSrl) {
                    mSrl.finishRefresh(2000);
                    mSrl.setNoMoreData(false);
                }
            }
        });

        mCommonAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                itemClick(adapter,view,position);
            }
        });

        mPage = 1;
        getData(true);
    }

    protected void itemClick(BaseQuickAdapter adapter, View view, int position) {

    }




    protected void setData(List<T> data) {
        if (isrefresh) {
            if (null != mCommonAdapter && data != null) {
                mCommonAdapter.setNewData(data);
                if (data.size() == 0) {

                        RxToast.showToast("暂无数据");

                }
            } else if (data == null) {
                RxToast.showToast("暂无数据");
            }

        } else {
            if (null != mCommonAdapter && data != null) {
                if (data.size() > 0 ) {
                    mCommonAdapter.addData(data);
                } else {
                    mSrl.setNoMoreData(true);
                }
            }
        }


    }

    protected abstract void getData(boolean refresh);

    protected abstract int getItemLayout();

    protected abstract void bind(BaseViewHolder holder, T item);

    @Override
    protected int getLayout() {
        return R.layout.common_refresh_fragment;
    }

    private  class CommonAdapter extends BaseQuickAdapter<T,BaseViewHolder>{
        public CommonAdapter(int layoutResId, @Nullable List<T> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, T item) {
            bind(helper,item);
        }
    }

}
